package com.cy.pj.notice.web.controller;

import com.cy.pj.common.util.PageUtil;
import com.cy.pj.notice.pojo.SysNotice;
import com.cy.pj.notice.service.SysNoticeService;
import com.cy.pj.notice.web.pojo.JsonResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 此对象为公告模块控制层处理对象
 * 1.请求url
 */
//@ResponseBody
//@Controller
@RestController
@RequestMapping("/notice/")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    //请求url：http://localhost/notice(80端口可省却端口号)
    //http://localhost/notice/findNotice/?title=加课
//    @GetMapping("/findNotice")
    @GetMapping
    public JsonResult doFindNotices(SysNotice sysNotice) {
        return new JsonResult(PageUtil.startPage().doSelectPageInfo(() -> sysNoticeService.findNotices(sysNotice)));
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Long id) throws Exception {//@PathVariable，接收URL中占位符{？？？}的值
        return new JsonResult("ID查找成功！", sysNoticeService.findById(id));
    }

    //新增最好用@PostMapping
    @PostMapping
    public JsonResult doInsertNotice(@RequestBody SysNotice sysNotice) {//@RequestBody注解描述参数，将Json字符串转换成参数类型的对象
        sysNoticeService.saveNotice(sysNotice);
        return new JsonResult("添加成功！");
    }

    @PutMapping
    public JsonResult doUpdateNotice(@RequestBody SysNotice sysNotice) {
        return new JsonResult("更新成功!", sysNoticeService.updateNotice(sysNotice));
    }

    @DeleteMapping("{id}")
    public JsonResult doDeleteNotice(@PathVariable Long... id) {
        return new JsonResult("删除成功", sysNoticeService.deleteById(id));
    }


}













